#include<iostream>
#include"Log.hpp"
#include"RingQueue.hpp"
#include"Task.hpp"
#include<pthread.h>
#include<ctime>
using namespace std;

void* Productor(void* args)
{
    RingQueue<Task>* rq = static_cast<RingQueue<Task>*>(args);

    while(true)
    {
        int data1 = rand() % 10;
        usleep(rand() % 123);

        int data2 = rand() % 10;
        usleep(rand() % 123); 

        char oper = opers[rand() % (opers.size())];
        Task t(data1, data2, oper);
        cout << "productor task: " << t.PrintTask() << endl;

        rq->Push(t);
    }
    return nullptr;
}

void* consumer(void* args)
{
     RingQueue<Task>* rq = static_cast<RingQueue<Task>*>(args);
     while(true)
     {
        Task t;
        rq->Pop(&t);

        t.Run();
        cout << "consumer done, data is : " << t.PrintResult() << endl;
     }
}

int main()
{
   /*  Log log;
    log.LogMessage(Debug, "hello %d, %s, %f", 10, "study", 3.14);
    log.LogMessage(warning, "hello %d, %s, %f", 10, "study", 3.14);
    log.LogMessage(Error, "hello %d, %s, %f", 10, "study", 3.14);
    log.LogMessage(Info, "hello %d, %s, %f", 10, "study", 3.14);
     */
    

    srand((uint64_t)time(nullptr) ^ pthread_self());
    pthread_t c[3], p[2];

    RingQueue<Task>* rq = new RingQueue<Task>();

    pthread_create(&p[0], nullptr, Productor, rq);
    pthread_create(&p[1], nullptr, Productor, rq);

    pthread_create(&c[0], nullptr, consumer, rq);
    pthread_create(&c[1], nullptr, consumer, rq);
    pthread_create(&c[2], nullptr, consumer, rq);

    pthread_join(p[0], nullptr);
    pthread_join(p[1], nullptr);
    pthread_join(c[0], nullptr);
    pthread_join(c[1], nullptr);
    pthread_join(c[2], nullptr);

    return 0;
}